/**
 * 
 */
package info.niwota.ziplock;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.text.format.DateFormat;

/**
 * @author qiangli
 * 
 */
public class Utils {
	private static final int THUMBNAIL_WIDTH = 96;

	private static final int THUMBNAIL_HEIGHT = 96;

	private static final int ICON_WIDTH = 48;

	private static final int ICON_HEIGHT = 49;

	/**
	 * 
	 */
	private Utils() {

	}

	public static void thumbnail(InputStream in, OutputStream out, int width,
			int height) {
		Bitmap image = BitmapFactory.decodeStream(in);
		int w = image.getWidth();
		int h = image.getHeight();
		float scalew = ((float) width) / w;
		float scaleh = ((float) height) / h;
		float scale = Math.min(scalew, scaleh);
		Matrix matrix = new Matrix();
		matrix.postScale(scale, scale);
		Bitmap resized = Bitmap.createBitmap(image, 0, 0, w, h, matrix, true);
		resized.compress(Bitmap.CompressFormat.JPEG, 50, out);
	}

	public static void thumbnail(InputStream in, OutputStream out) {
		thumbnail(in, out, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
	}

	public static void icon(InputStream in, OutputStream out) {
		thumbnail(in, out, ICON_WIDTH, ICON_HEIGHT);
	}

	public static String createName() {
		long date = System.currentTimeMillis();
		return DateFormat.format("yyyy-MM-dd_kkmmss", date).toString();
	}

	public static String createName(String prefix, String ext) {
		long date = System.currentTimeMillis();
		return prefix + DateFormat.format("yyyy-MM-dd_kkmmss", date) + ext;
	}

	public static String sanitize(String name) {
		// replace space, ~, :,
		name = name.replaceAll("~", "_");
		name = name.replaceAll(" ", "_");
		name = name.replaceAll(":", "_");
		return name;
	}

	public static String forDisplay(String name) {
		name = name.replaceAll("_", " ");
		return name;
	}

	public static CharSequence readResource(Activity activity, int id) {
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(activity
					.getResources().openRawResource(id)));
			String line;
			StringBuilder buffer = new StringBuilder();
			while ((line = in.readLine()) != null)
				buffer.append(line).append('\n');
			return buffer;
		} catch (IOException e) {
			return "";
		} finally {
			closeStream(in);
		}
	}

	private static void closeStream(Closeable s) {
		if (s != null) {
			try {
				s.close();
			} catch (IOException e) {
			}
		}
	}

	public static boolean isActivityAvailable(Context context, String action) {
		final PackageManager packageManager = context.getPackageManager();
		final Intent i = new Intent(action);
		List<ResolveInfo> l = packageManager.queryIntentActivities(i,
				PackageManager.MATCH_DEFAULT_ONLY);
		return (l.size() > 0);
	}
	
	public static CharSequence readResource(Context ctx, int id) {
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(ctx.getResources()
					.openRawResource(id)));
			String line;
			StringBuilder buffer = new StringBuilder();
			while ((line = in.readLine()) != null)
				buffer.append(line).append('\n');
			return buffer;
		} catch (IOException e) {
			return "";
		} finally {
			closeStream(in);
		}
	}
}
